package com.project.slsx.activity;

import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.project.slsx.Constant.Constans;
import com.project.slsx.R;
import com.project.slsx.adapter.OrderListAdapter;
import com.project.slsx.bean.OrderListBean;
import com.project.slsx.utils.UserManager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

public class OrderListActivity extends BaseActivity implements View.OnClickListener {

    private static final String TAG = OrderListActivity.class.getSimpleName();
    private TextView title;
    private TextView tvRight;
    private RelativeLayout back;
    private RecyclerView rv;
    private SmartRefreshLayout sfl;

    private int page = 1;
    private int page_size = 10;
    private OrderListAdapter adapter;
    private TextView goodsNum;
    private TextView sumPrice;


    @Override
    public int getLayoutId() {
        return R.layout.activity_order_list;
    }

    @Override
    public void initView() {
        title = findViewById(R.id.tv_title);
        tvRight = findViewById(R.id.tv_right);
        back = findViewById(R.id.rl_back);
        back.setOnClickListener(this);
        tvRight.setVisibility(View.INVISIBLE);
        title.setText("订单");

        sfl = findViewById(R.id.sfl);
        rv = findViewById(R.id.recyclerview);
        rv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new OrderListAdapter(R.layout.layout_item_order_list);
        rv.setAdapter(adapter);
        sfl.autoRefresh();
        sfl.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                refresh(refreshLayout.getLayout(), false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                refresh(refreshLayout.getLayout(), true);
            }
        });

    }

    /**
     * @param b true  下拉刷新 false 上拉加载更多
     */
    private void refresh(ViewGroup vp, final boolean b) {
        vp.postDelayed(() -> {
            if (b) {
                sfl.setEnableLoadMore(false);
                page = 1;
                getData(b);
            } else {
                ++page;
                getData(b);
            }
        }, 10);
    }

    private void getData(final boolean b) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                HttpParams hp = new HttpParams();
                hp.put("op", "orderlist");
                hp.put("token", UserManager.getUser().getToken());
                hp.put("uniacid", UserManager.getUser().getUniacid());
                hp.put("sid", UserManager.getUser().getSid());
                hp.put("page", page);
                hp.put("page_size", page_size);
                OkGo.<String>post(Constans.BASEURL)
                        .tag("orderlist")
                        .params(hp)
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(Response<String> response) {
                                Log.i(TAG, "onSuccess: " + response.body());
                                OrderListBean orderListBean = JSONObject.parseObject(response.body(), OrderListBean.class);
                                if (orderListBean.getErrno() == Constans.SUCCESS) {
                                    if (orderListBean.getResult() != null && orderListBean.getResult().size() > 0) {
                                        if (b) {
                                            adapter.setNewData(orderListBean.getResult());
                                        } else {
                                            adapter.addData(orderListBean.getResult());
                                        }
                                    } else {
                                        showToast(mContext, "无数据");
                                    }
                                } else {
                                    showToast(mContext, orderListBean.getErrmsg());
                                }
                            }

                            @Override
                            public void onError(Response<String> response) {
                                super.onError(response);
                                Log.i(TAG, "onError: ");
                            }

                            @Override
                            public void onFinish() {
                                super.onFinish();
                                Log.i(TAG, "onFinish: ");
                                if (b) {
                                    sfl.finishRefresh();
                                    sfl.setEnableLoadMore(true);
                                } else {
                                    sfl.finishLoadMore();
                                    sfl.setEnableRefresh(true);
                                }
                            }
                        });
            }
        }, 10);
    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        switch (id) {
            case R.id.rl_back:
                finish();
                break;

        }
    }
}
